//
//  LPOrganizationReportViewController.m
//  ShangHaiHuDongApp
//
//  Created by 刘应平 on 2017/7/23.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "LPOrganizationReportViewController.h"
#import "LPCostomLabelAndTextFieldAndImageView.h"
#import "JCAlertView.h"
#import "ZJSILocationView.h"
#import "YBPopupMenu.h"
#import "LPOrangeStreetModel.h"
#import "LPOrangenizeTypeModel.h"
#import "XLSlideMenu.h"
#import "ZJTabBarController.h"
#import "LPLeftViewController.h"
#import "ZJLoginJudgeManager.h"
#import "LoginViewController.h"
#import "LPOrganizeReportChooseStreetViewController.h"
#define CellID @"OrganizationViewSelectController"

@interface LPOrganizationReportViewController ()<ZJSILocationViewDelegate,LPCostomLabelAndTextFieldAndImageViewDelegate,YBPopupMenuDelegate,UIScrollViewDelegate>
/**  组织名称  */
@property (strong, nonatomic) LPCostomLabelAndTextFieldAndImageView  *organizationName;
/**  组织类别  */
@property (strong, nonatomic) LPCostomLabelAndTextFieldAndImageView  *organizationType;

/**  活动地址  */
@property (strong, nonatomic) ZJSILocationView  *actAddress;
/**  经度  */
@property (strong, nonatomic) NSString  *lngString;
/**  纬度  */
@property (strong, nonatomic) NSString  *latString;

/**  报道街道  */
@property (strong, nonatomic) LPCostomLabelAndTextFieldAndImageView  *reportStreet;
/**  联系人  */
@property (strong, nonatomic) LPCostomLabelAndTextFieldAndImageView  *contantPerson;
/**  电话  */
@property (strong, nonatomic) LPCostomLabelAndTextFieldAndImageView  *phoneNumber;
/**  邮编  */
@property (strong, nonatomic) LPCostomLabelAndTextFieldAndImageView  *postcode;
/**  电子邮箱*/
@property (strong, nonatomic) LPCostomLabelAndTextFieldAndImageView  *mailbox;
/**  提交  */
@property (strong, nonatomic) UIButton  *sendButton;
/**  scrollView  */
@property (strong, nonatomic) UIScrollView  *backScrollView;

/** tableView */
@property (nonatomic, strong) UITableView *tableView;
/** 组织类别数据 */
@property (nonatomic, strong) NSArray *dataArray;
/**  选中的组织类别model  */
@property (strong, nonatomic) LPOrangenizeTypeModel  *orgTypeModel;

/**  报道街道数据  */
@property (strong, nonatomic) NSArray  *dataSouce;
/**  街道model  */
@property (strong, nonatomic) LPOrangeStreetModel  *streetModel;


/**  判断是选择的是组织类别还是报道街道  */
@property (assign, nonatomic) NSInteger  index;
/**  类别的偏移量  */
@property (assign, nonatomic) CGFloat  typeOffSet;
/**  组织类别显示个数  */
@property (assign, nonatomic) NSInteger  typeNumbers;

/**  街道偏移量  */
@property (assign, nonatomic) CGFloat  streetOffSet;
/**  街道显示数量  */
@property (assign, nonatomic) NSInteger  streetNumbers;


@end

@implementation LPOrganizationReportViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.typeOffSet = 0.0;
    self.streetOffSet = 0.0;
    self.titlelabel.text = @"组织报到";
    //    self.orgTypeModel = [LPOrangenizeTypeModel new];
    //    self.streetModel = [LPOrangeStreetModel new];
    [self.view addSubview:self.backScrollView];
    [self.backScrollView addSubview:self.organizationName];
    [self.backScrollView addSubview:self.organizationType];
    [self.backScrollView addSubview:self.actAddress];
    [self.backScrollView addSubview:self.reportStreet];
    [self.backScrollView addSubview:self.contantPerson];
    [self.backScrollView addSubview:self.phoneNumber];
    [self.backScrollView addSubview:self.postcode];
    [self.backScrollView addSubview:self.sendButton];
    self.backScrollView.contentSize = CGSizeMake(self.view.width, self.sendButton.bottom + 60 * [UIScreen zj_screenHScale]);
    [self fuzhi];
    [self netWorking_orgType];
    self.typeNumbers = (self.backScrollView.height - self.organizationType.bottom ) / 32;
    self.streetNumbers = (self.backScrollView.height - self.reportStreet.bottom ) / 32;
    
}

/**  将带过来的值显示出来  */
- (void)fuzhi{
    self.phoneNumber.secondTextField.text = self.model.MOBILE;
    self.contantPerson.secondTextField.text = self.model.PARTYMEMBER_NAME;

}

#pragma mark - 网络请求
/**  请求组织类别  */
- (void)netWorking_orgType{
    __weak typeof(self) weakSelf = self;
    [NetWorkingManager postWithUrlString:[NetWorkingURL getorgloginactionPudongLogingetOrgTypes] Parameter:nil Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            weakSelf.dataArray = [NSArray yy_modelArrayWithClass:[LPOrangenizeTypeModel class] json:[object objectForKey:@"ORG_TYPES"]];
            [weakSelf initOrgType];
            weakSelf.dataSouce = [NSArray yy_modelArrayWithClass:[LPOrangeStreetModel class] json:[object objectForKey:@"ORG_STREETS"]];
            [weakSelf initViewUI];
            
        }
    } Fail:^(id object) {
        
    }];
}

/**  初始化组织  */
- (void)initOrgType{
    if (self.dataArray.count > 0) {
        LPOrangenizeTypeModel *model = self.dataArray[0];
        self.organizationType.secondTextField.text = model.CODE_NAME;
        self.orgTypeModel = model;
    }
    
}


////初始化街道赋值
- (void)initViewUI{
    if (self.dataSouce.count > 0) {
        LPOrangeStreetModel *model = self.dataSouce[0];
        self.reportStreet.secondTextField.text = model.DEPTABBREVIATION;
        self.streetModel = model;
    }
}



#pragma mark - 点击事件
- (void)clickButton{
    if (self.organizationName.secondTextField.text.length == 0) {
        [YJProgressHUD showSuccess:@"请输入组织名" inview:self.view];
        return;
    }
    if (self.organizationType.secondTextField.text.length == 0) {
        [YJProgressHUD showSuccess:@"请选择组织类别" inview:self.view];
        return;
    }
    if (self.actAddress.loactionLabel.text.length == 0) {
        [YJProgressHUD showSuccess:@"请输入党组织地址" inview:self.view];
        return;
    }
    if (self.reportStreet.secondTextField.text.length == 0) {
        [YJProgressHUD showSuccess:@"请选择报到街道园区" inview:self.view];
        return;
    }
    if (self.contantPerson.secondTextField.text.length == 0) {
        [YJProgressHUD showSuccess:@"请输入联系人" inview:self.view];
        return;
    }
    if (![CManager zj_validateMobile:self.phoneNumber.secondTextField.text]) {
        [YJProgressHUD showMessage:@"请输入正确的电话" inView:self.view];
        return;
    }
    
    if (self.postcode.secondTextField.text.length == 0 && self.postcode.secondTextField.text.length != 6) {
        [YJProgressHUD showSuccess:@"请输入正确的邮编" inview:self.view];
        return;
    }
    
    [YJProgressHUD showMessage:@"报到中..." duration:10 view:self.view];
    __weak typeof(self) weakSelf = self;
    NSMutableDictionary *paramer = [NSMutableDictionary dictionary];
    paramer[@"DEPT_NAME"] = self.organizationName.secondTextField.text;
    paramer[@"DEPTCONTACTS_MOBILE"] = self.phoneNumber.secondTextField.text;
    paramer[@"DEPTCONTACTS_ADDRESS"] = self.actAddress.loactionLabel.text;
    paramer[@"DEPT_DEADDRESS_COORDINATE_X"] = self.latString;
    paramer[@"DEPT_DEADDRESS_COORDINATE_Y"] = self.lngString;
    paramer[@"DEPTCONTACTS_NAME"] = self.contantPerson.secondTextField.text;
    paramer[@"ZIPCODE"] = self.postcode.secondTextField.text;
    paramer[@"DEPTTYPE_DSCODE_VALUE"] = self.orgTypeModel.CODE_VALUE;
    paramer[@"DEPTSECRETARY_ACCOUNTID"] = [NSString isNilStringReturnString:self.model.ACCOUNT_ID];
    paramer[@"LINKAGE_FID"] = self.streetModel.ID;
    [NetWorkingManager postWithUrlString:[NetWorkingURL getpudongorgactionMaintainDeptInfoMobile] Parameter:paramer Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
         [YJProgressHUD hide];
        if ([model.STATE isEqualToString:@"20000"]) {
            [MobClick event:[UMMStatisticsString orgRegister]];
            [YJProgressHUD showSuccess:@"报到成功" inview:weakSelf.view];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.7 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [weakSelf backViewControllerType];
            });
        }else{
        
            [YJProgressHUD showSuccess:@"报到失败" inview:weakSelf.view];
        }
    } Fail:^(id object) {
        [YJProgressHUD hide];
        [YJProgressHUD showSuccess:@"报到失败" inview:weakSelf.view];
    }];
    
}


//退回方式
- (void)backViewControllerType{

    [self.navigationController popToRootViewControllerAnimated:YES];

}


#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    self.typeOffSet = scrollView.contentOffset.y;
    self.streetOffSet = scrollView.contentOffset.y;

}

#pragma mark - LPCostomLabelAndTextFieldAndImageViewDelegate
- (void)LPCostomLabelAndTextFieldAndImageWith:(LPCostomLabelAndTextFieldAndImageView *)view click:(BOOL)click{
    NSInteger showCount = 0;
    NSMutableArray *array = [NSMutableArray array];
    if (view.tag == 1) {
        /**  组织类型  */
        _index = 1;
        for (LPOrangenizeTypeModel *model in self.dataArray) {
            [array addObject:model.CODE_NAME];
        }
        showCount = self.typeNumbers;
        NSInteger zheng = self.typeOffSet / 32;
        showCount += zheng;

    }else{
        /**  报道街道  */
        _index = 2;
        for (LPOrangeStreetModel *model in self.dataSouce) {
            [array addObject:model.DEPTABBREVIATION];
        }
        showCount = self.streetNumbers;
        NSInteger zheng = self.streetOffSet / 32;
        showCount += zheng;
      
        
    }
    if ( array.count == 0) {
        return;
    }
    [YBPopupMenu showRelyOnView:view titles:array icons:@[] menuWidth:[UIScreen zj_screenWith] - 30 * [UIScreen zj_screenWScale] showCount:showCount delegate:self];
    
}

#pragma mark - YBPopupMenuDelegate
- (void)ybPopupMenuDidSelectedAtIndex:(NSInteger)index ybPopupMenu:(YBPopupMenu *)ybPopupMenu{
    if (_index == 1) {
        LPOrangenizeTypeModel *model = self.dataArray[index];
        self.organizationType.secondTextField.text = model.CODE_NAME;
        self.orgTypeModel = model;
        
    }else{
        LPOrangeStreetModel *model = self.dataSouce[index];
        self.reportStreet.secondTextField.text = model.DEPTABBREVIATION;
        self.streetModel = model;

    }
    
}

#pragma mark - ZJSILocationViewDelegate
- (void)ZJSILocationViewDelegate_latitude:(NSString *)latitude longitude:(NSString *)longitude{
    self.latString = latitude;
    self.lngString = longitude;
}

#pragma mark - setter
- (LPCostomLabelAndTextFieldAndImageView *)organizationName{
    if (!_organizationName) {
        _organizationName = [[LPCostomLabelAndTextFieldAndImageView alloc] initWithFrame:CGRectMake(0,  20 * [UIScreen zj_screenHScale], self.view.width, 80 * [UIScreen zj_screenHScale]) title:@"组织名称 :" placehold:@"" imageString:@""];
        _organizationName.secondTextField.userInteractionEnabled = YES;
        _organizationName.maxNumber = 50;
    }
    return _organizationName;
}

- (LPCostomLabelAndTextFieldAndImageView *)organizationType{
    if (!_organizationType) {
        _organizationType = [[LPCostomLabelAndTextFieldAndImageView alloc] initWithFrame:CGRectMake(0,  self.organizationName.bottom + 10 * [UIScreen zj_screenHScale], self.view.width, 80 * [UIScreen zj_screenHScale]) title:@"组织类别 :" placehold:@"" imageString:@"sign_select_more"];
        _organizationType.tag = 1;
        _organizationType.delegate = self;
    }
    return _organizationType;
}

- (ZJSILocationView *)actAddress{
    if (!_actAddress) {
        _actAddress = [[ZJSILocationView alloc]initWithFrame:CGRectMake(0, self.organizationType.bottom + 5 * [UIScreen zj_screenHScale], self.view.width, 90  * [UIScreen zj_screenHScale]) title:@"党组织地址 :"];
        _actAddress.delegate = self;
        _actAddress.imageString = @"float_map";
    }
    return _actAddress;
}


- (LPCostomLabelAndTextFieldAndImageView *)reportStreet{
    if (!_reportStreet) {
        _reportStreet = [[LPCostomLabelAndTextFieldAndImageView alloc] initWithFrame:CGRectMake(0, self.actAddress.bottom + 10 * [UIScreen zj_screenHScale], self.view.width, 80 * [UIScreen zj_screenHScale]) title:@"报到街镇园区 :" placehold:@"" imageString:@"sign_select_more"];
        _reportStreet.tag = 2;
        _reportStreet.delegate = self;
    }
    return _reportStreet;
}

- (LPCostomLabelAndTextFieldAndImageView *)contantPerson{
    if (!_contantPerson) {
        _contantPerson = [[LPCostomLabelAndTextFieldAndImageView alloc] initWithFrame:CGRectMake(0, self.reportStreet.bottom + 10 * [UIScreen zj_screenHScale], self.view.width, 80 * [UIScreen zj_screenHScale]) title:@"联系人 :" placehold:@"" imageString:@""];
        _contantPerson.secondTextField.userInteractionEnabled = YES;
        _contantPerson.maxNumber = 50;
    }
    return _contantPerson;
}
- (LPCostomLabelAndTextFieldAndImageView *)phoneNumber{
    if (!_phoneNumber) {
        _phoneNumber = [[LPCostomLabelAndTextFieldAndImageView alloc] initWithFrame:CGRectMake(0, self.contantPerson.bottom + 10 * [UIScreen zj_screenHScale], self.view.width, 80 * [UIScreen zj_screenHScale]) title:@"电话 :" placehold:@"" imageString:@""];
        _phoneNumber.secondTextField.userInteractionEnabled = YES;
        _phoneNumber.secondTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        _phoneNumber.maxNumber = 11;
        
    }
    return _phoneNumber;
}
- (LPCostomLabelAndTextFieldAndImageView *)postcode{
    if (!_postcode) {
        _postcode = [[LPCostomLabelAndTextFieldAndImageView alloc] initWithFrame:CGRectMake(0, self.phoneNumber.bottom + 10 * [UIScreen zj_screenHScale], self.view.width, 80 * [UIScreen zj_screenHScale]) title:@"邮编 :" placehold:@"" imageString:@""];
        _postcode.secondTextField.userInteractionEnabled = YES;
        _postcode.secondTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        _postcode.maxNumber = 6;
        
    }
    return _postcode;
}


- (UIButton *)sendButton{
    if (!_sendButton) {
        _sendButton = ({
            UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(40 * [UIScreen zj_screenWScale], self.postcode.bottom + 40 * [UIScreen zj_screenHScale], self.view.width - 80 * [UIScreen zj_screenWScale], 43 * [UIScreen zj_screenHScale])];
            [button setTitle:@"确认" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            button.titleLabel.textAlignment = NSTextAlignmentCenter;
            button.titleLabel.font = LYPFONT(15);
            button.backgroundColor = [UIColor themeColor];
            [button addTarget:self action:@selector(clickButton) forControlEvents:UIControlEventTouchUpInside];
            button.layer.cornerRadius = 3;
            button.layer.masksToBounds = YES;
            button;
            
        });
    }
    return _sendButton;
}

- (UIScrollView *)backScrollView{
    if (!_backScrollView) {
        _backScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, LP_TopHeight, self.view.width, self.view.height - LP_TopHeight )];
        _backScrollView.contentOffset = CGPointMake(0, 0);
        _backScrollView.showsVerticalScrollIndicator = NO;
        _backScrollView.delegate = self;
    }
    return _backScrollView;
    
}
@end
